package stock.testSB;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import stock.StockContrl;
import stock.StockQS;

public class StockDataUpdater implements Runnable {

	public static boolean isTimeInRange() {
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);

		// 将9:15转换为分钟数，9 * 60+15 = 555分钟
		int startTimeInMinutes = 9 * 60 + 15;
		// 将10:00转换为分钟数，10 * 60 = 600分钟
		int endTimeInMinutes = 10 * 60;

		int currentTimeInMinutes = hour * 60 + minute;
		return currentTimeInMinutes >= startTimeInMinutes && currentTimeInMinutes < endTimeInMinutes;
	}

	@Override
	public void run() {
		while (true) {

//			if (!isTimeInRange()) {
//				System.out.println("当前时间不在9:15到10:00之间");
//			}

			try {
				Thread.sleep(5000);
				// String codeall = "sz002181,sh603270";
				JSONArray jsonall = StockContrl.getCcc(String.join(",", StockQS.shoubanlist), 1f);
				System.out.println("-获取数据---");
				for (int i = 0; i < jsonall.size(); i++) {
					String code = jsonall.getJSONObject(i).getString("code");

					SimpleDateFormat sdf = new SimpleDateFormat("mmss");
					Date now = new Date();
					String formattedDate = sdf.format(now);
					TimeZDF tz = new TimeZDF();
					tz.setTime(Long.parseLong(formattedDate));
					DecimalFormat df = new DecimalFormat("0.00");
					String result = df.format(Float.parseFloat(jsonall.getJSONObject(i).getString("zhangdiefu")));
					tz.setZhangdiefu(result);
					for (int j = 0; j < SB.listall.size(); j++) {
						if (SB.listall.get(j).getZdfList().size() >= 63) {
							for (int j2 = SB.listall.get(j).getZdfList().size() - 1; j2 >= 0; j2--) {
								if (j2 <= 61) {
									break;
								}
								if (j2 >= 62) {
									SB.listall.get(j).getZdfList().remove(j2);
								}
							}
						}
						if (code.equals(SB.listall.get(j).getName())) {
							SB.listall.get(j).getZdfList().add(tz);
							//SB.listall.get(j).setTime(Long.parseLong(formattedDate)+"");
							// System.out.println(code);
							// System.out.println(JSONArray.fromObject(SB.listall.get(j).getZdfList()).toString());
							break;
						}
					}

				}
//				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
